<?php

namespace app\common\Utils;

use JPush\Client;

class Jpush
{
    protected $client;

    public function __construct(){

        //极光推送appkey
        $app_key = config('param')['jpush']['app_key'];
        //极光推送master_secret
        $master_secret = config('param')['jpush']['master_secret'];

        $this->client = new Client($app_key, $master_secret,'../runtime/log/jpush.log');
    }

    //获取alias和tags
    public function getDevices($registrationID)
    {

        $result = $this->client->device()->getDevices($registrationID);

        return $result;

    }

    //添加tags
    public function addTags($registrationID, $tags)
    {

        $result = $this->client->device()->addTags($registrationID, $tags);

        return $result;

    }

    //移除tags
    public function removeTags($registrationID, $tags)
    {

        $result = $this->client->device()->removeTags($registrationID, $tags);

        return $result;

    }

    //标签推送
    public function push($tag, $alert)
    {

        $tags = implode(",", $tag);

        $this->client->push()
            ->setPlatform(array('ios', 'android'))
            ->addTag($tags)//标签
            ->setNotificationAlert($alert)//内容
            ->send();

    }

    //别名推送
    public function push_a($alias, $alert)
    {

        $alias = implode(",", $alias);

        $this->client->push()
            ->setPlatform(array('ios', 'android'))
            ->addAlias($alias)//别名
            ->setNotificationAlert($alert)//内容
            ->send();

    }

}